<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\Product;
use common\models\ProductCategory;
use common\models\ProductGallery;
use common\models\Category;
use backend\assets\AppAsset;

/* @var $this yii\web\View */
/* @var $model common\models\Product */
/* @var $form yii\widgets\ActiveForm */
?>

<style type="text/css">
    #para{list-style-type:none;padding-left: -20px;}
    #para li {float: left;margin: 0 10px 10px 0px;}
    #add_para{margin-left: 40px;}
</style>
<div class="product-form">  

<?php $model->price_unit = $model->price_unit?$model->price_unit:1;?>

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'brand_id')->dropDownList(Product::listBrand()); ?>

    <?php
        $query = Category::find()->select('id');

        $lv = $query->where(['is_show'=>1,'level'=>1])->column();

        $lv1 = $query->where(['is_show'=>1,'level'=>2,'parent_id'=>72])->count();
        $lv2 = $query->where(['is_show'=>1,'level'=>2,'parent_id'=>95])->count();
        $lv3 = $query->where(['is_show'=>1,'level'=>2,'parent_id'=>96])->count();
        //查询当前商品的分类选项
        $checked = product::ProductCategoryname($model->id);
        $model->category_name = array_keys($checked);
        echo $form->field($model, 'category_name')->checkboxList(Product::listCategory()); 
    ?>

<?php if(Yii::$app->request->get('id')) {?>
    <br>
    <!-- The fileinput-button span is used to style the file input field as button -->
    <span class="btn btn-success fileinput-button">
        <i class="glyphicon glyphicon-plus"></i>
        <span>上传商品图片</span>
        <!-- The file input field used as target for the file upload widget -->
        <input id="fileupload" type="file" name="files[]" multiple>
    </span><font color='red' style='margin-left: 20px'>tips：单击图片删除</font>
    <br>
    <br>
    <!-- The global progress bar -->
    <div id="progress" class="progress">
        <div class="progress-bar progress-bar-success"></div>
    </div>
    <!-- The container for the uploaded files -->
    <div id="files" class="files"></div>
    <br>
<?php }?>
    <div style="clear:both;"></div>
    <div id="gallery_warp" style="margin:-30px 0 30px 0;">

    <?php
        $cid = Yii::$app->request->get('id');
        $pic = ProductGallery::find()->where(['product_id' => $cid])->orderBy('id DESC')->asArray()->all();
        //var_dump($pic);exit;
        if ($cid && $pic) {
            foreach ($pic as $key => $g) {
                echo '<img  src="'.$g['img_url'].'!s100" class="img-thumbnail" name="'.$g['id'].'" style="cursor:pointer"/>';
            }
        }
    ?>
    </div>
    <div style="clear:both;"></div>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'keywords')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'img_url')->textInput(['maxlength' => true,'style'=>'width:60%;display: inline; 
    margin:0 20px 10px;']) ?>

    <?= $form->field($model, 'des')->textarea(['style' => 'width:760px;height:500px;']) ?>

    <?= $form->field($model, 'parameter')->textInput(['maxlength' => true,'type' => 'hidden','id' => 'parameter']) ?>

    <ul id="para" style="margin-left:-40px;">
        <?php if (Yii::$app->request->get('id') && $model->parameter): ?>
            <?php $ary = json_decode($model->parameter,true); ?>
            <?php if ($ary && count($ary)>1): ?>
                <?php  $i = 0; ?>
                <?php foreach ($ary as $k => $v): ?>
                    <li>参数名：<input type="text" value="<?=array_keys($v)[0]?>">&nbsp;</li>
                    <li>参数值：<input type="text" value="<?=$v[array_keys($v)[0]]?>">&nbsp;</li>
                    <?php if ($i>0 && ($i+1)%2 == 0): ?>
                        <div style="clear:both"></div>
                    <?php endif ?>
                    <?php $i++; ?>
                <?php endforeach ?><div style="clear:both"></div>
            <?php else: ?>  <?php $in = array_keys($ary)[0];  $va = $ary[$in]; ?>
                <li>参数名：<input type="text" value="<?=array_keys($va)[0]?>">&nbsp;</li>
                <li>参数值：<input type="text" value="<?=$va[array_keys($va)[0]]?>">&nbsp;</li>
                <li>参数名：<input type="text" value="">&nbsp;</li>
                <li>参数值：<input type="text" value="">&nbsp;</li><div style="clear:both"></div>
            <?php endif ?>
            
        <?php else: ?>
            <li>参数名：<input type="text" value="">&nbsp;</li>
            <li>参数值：<input type="text" value="">&nbsp;</li>
            <li>参数名：<input type="text" value="">&nbsp;</li>
            <li>参数值：<input type="text" value="">&nbsp;</li>
            <div style="clear:both"></div>
        <?php endif ?>
    </ul>
    <div style="clear:both"></div>


    <?= $form->field($model, 'market_price')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'product_price')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'promotion_price')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'price_unit')->textInput(['maxlength' => true]) ?>
    <?php // $form->field($model, 'price_unit')->dropDownList(Product::itemAlias('price_unit')) ?>
    
    <?= $form->field($model, 'spec')->textInput() ?>

    <?= $form->field($model, 'weight')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'weight_unit')->dropDownList(Product::itemAlias('weight_unit')) ?>

    <?= $form->field($model, 'company_id')->dropDownList(Product::listCompany(0)) ?>

    <?= $form->field($model, 'stock')->textInput() ?>

    <?php // $form->field($model, 'send_sample')->dropDownList(Product::itemAlias('send_sample')) ?>

    <?= $form->field($model, 'sort_numer')->textInput() ?> 

    <?= $form->field($model, 'hot_sort')->textInput() ?> 

    <?= $form->field($model, 'serial_number')->textInput(['maxlength' => true]) ?>

    <?php // $form->field($model, 'full_cut_shipping_free')->dropDownList(Product::itemAlias('full_cut_shipping_free')) ?>

    <?php // $form->field($model, 'supply')->dropDownList(Product::itemAlias('supply')) ?>

    <?= $form->field($model, 'is_show')->dropDownList(Product::itemAlias('is_show')) ?>


    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '添加' : '修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php
$this->registerCssFile('/css/umeditor/themes/default/css/umeditor.css');

$this->registerJsFile('/css/umeditor/umeditor.config.js',[AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);
$this->registerJsFile('/css/umeditor/umeditor.min.js',[AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);
$this->registerJsFile('/css/umeditor/lang/zh-cn/zh-cn.js',[AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);

// AppAsset::register($this);  
// AppAsset::addCss($this,'/css/umeditor/themes/default/css/umeditor.css');
// //AppAsset::addScript($this,'/css/umeditor/third-party/jquery.min.js');
// AppAsset::addScript($this,'/css/umeditor/umeditor.config.js');
// AppAsset::addScript($this,'/css/umeditor/umeditor.min.js');
// AppAsset::addScript($this,'/css/umeditor/lang/zh-cn/zh-cn.js');
?>
<?php
AppAsset::addCss($this,'/jQuery_File_Upload/css/jquery.fileupload.css');
AppAsset::addScript($this,'/jQuery_File_Upload/js/vendor/jquery.ui.widget.js');
AppAsset::addScript($this,'/jQuery_File_Upload/js/jquery.iframe-transport.js');
AppAsset::addScript($this,'/jQuery_File_Upload/js/jquery.fileupload.js');
?>
<?php $this->beginBlock('footer_js') ?>
$(function(){
    $('#product-category_name label').eq(2).after('<br/><br/>');

    var lv1 = <?=$lv1+2?>;
    var lv2 = <?=$lv1+$lv2+2?>;
    $('#product-category_name label').eq(lv1).after('<br/><br/>');
    $('#product-category_name label').eq(lv2).after('<br/><br/>');
    
});

/*jslint unparam: true */
/*global window, $ */
$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    //var url = '/jQuery_File_Upload/server/php/';

    var url = '/tool/alum?id=<?= $model->id;?>&use=1';
    $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                var img_text = '<a href="'+file.url+'" target="_blank"><img src="'+file.thumbnailUrl+'" class="img-thumbnail"></a>';
                $("#gallery_warp").prepend(img_text);
                //$(img_text).appendTo('#files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');

    //删除图片
    $('.img-thumbnail').on('click',function(){
        if(confirm('确定要删除这张图吗？')){
            $.post( "", { del:1,id:$(this).attr('name')},function(data){
                if(data){
                    alert('删除成功');
                    setTimeout("window.location.href = ''",100);
                }else{
                    alert('删除失败，请稍后再试');
                }
            });
        }
    });
});

jQuery(document).ready(function () {

    var um = UM.getEditor('product-des',{
        imageUrl:"/tool/umfile",
        imagePath:''
    });
});
//封面图
jQuery(document).ready(function () {
    var button = '<button type="button" data-toggle="modal" data-target="#myModal" id="upload_img_url" upload_field="product-img_url"  style="">上传</button>';
    $('#product-img_url').after(button);
    $('#upload_img_url').click(function(){
        var upload_field = $(this).attr('upload_field');
        $('#upload_img_iframe').attr('src','/tool/uploadupyun?upload_field='+upload_field);
    });
    

});

$(function(){
    var btn = '<input type="button" id="add_para" value="添加参数">';
    $('#parameter').siblings('label').after(btn);

    $('#add_para').on('click',function(){
        var zone = '<li style="float: left;margin: 0 10px 16px 0px;list-style-type:none">参数名：<input type="text" >&nbsp;</li><li style="float: left;margin: 0 10px 16px 0px;list-style-type:none">参数值：<input type="text" value="">&nbsp;</li><li style="float: left;margin: 0 10px 16px 0px;list-style-type:none">参数名：<input type="text" >&nbsp;</li><li style="float: left;margin: 0 10px 16px 0px;list-style-type:none">参数值：<input type="text" value="">&nbsp;</li><div style="clear:both"></div>';
        $('#para').append(zone);
    });

    $('.btn-success,.btn-primary').on('click',function(){
        var li = $('#para input');
        var list = '';
        for(var i = 0; i<li.length; i += 2){
            list += li.eq(i).val()+"::"+li.eq(i+1).val()+",,";
        }
        $('#parameter').val(list);
        return true;
    });

})
<?php $this->endBlock() ?>  
<?php $this->registerJs($this->blocks['footer_js'], \yii\web\View::POS_END); ?>